<template>
  <div class="outbox">
    <div class="form-box">
      <div class="logo"><img src="@/assets/logo.png" alt=""></div>
      <el-form :model="ruleForm" :rules="rules" class="demo-ruleForm" ref="ruleForm" status-icon>
        <el-form-item label="用户名" prop="username">
          <el-input
            autocomplete="off"
            placeholder="请输入用户名"
            type="password"
            v-model="ruleForm.username"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            autocomplete="off"
            placeholder="请输入密码"
            type="password"
            v-model="ruleForm.password"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>

        <el-form-item class="button">
          <el-button @click="submitForm('ruleForm')" class="submit" type="primary">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    var validatePass2 = (rule, value, callback) => {
      const reg = /^\d{6,}$/
      if (!reg.test(value)) {
        callback(new Error('请输入6位以上的密码'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, trigger: 'blur', message: '用户名不能为空' }
        ],
        password: [
          { min: 5, required: true, trigger: 'blur', message: '密码不能为空' },
        ]
      }
    };
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        console.log(valid)
        if (valid) {
          this.$store.dispatch('asynclogin', this.ruleForm)
          this.$router.push('/')
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm (formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
<style lang='less'  scoped>
.outbox {
  width: 100vw;
  height: 100vh;
  background-color: #2b4b6b;
  position: relative;
  .form-box {
    position: relative;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 15px;
    width: 450px;
    height: 300px;
    background-color: #fff;
    el-input{
      position: relative;
    }
    .el-icon-user-solid{
      position: absolute;
      left: 10px;
      top: 10px;
    }
    .logo {
      position: absolute;
      left: 50%;
      top: -50%;
      transform: translate(-50%, 50%);
      padding: 10px;
      width: 152px;
      height: 152px;
      border-radius: 50%;
      background-color: #fff;
      box-shadow: 0 0 10px #ddd;
      img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee
      }
    }
    .demo-ruleForm {
      margin-top: 50px;
      .button {
        float: right;
      }
    }
  }
}
</style>
